package com.vogue.flowmeter.api.retrofit;

import com.vogue.flowmeter.api.BleApiService;

import java.util.concurrent.TimeUnit;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.vogue.flowmeter.api.BleApiService.APP_DEFAULT_DOMAIN;

/**
 * 网络请求管理器
 */
public class NetWorkManager {
    private OkHttpClient mOkHttpClient;
    private OkHttpClient mDownloadOkHttpClient;
    private Retrofit mRetrofit;
    private Retrofit mDownloadRetrofit;
    private BleApiService mBleApiService;
    private BleApiService mDownloadBleApiService;


    private static class NetWorkManagerHolder {
        private static final NetWorkManager INSTANCE = new NetWorkManager();
    }

    public static final NetWorkManager getInstance() {
        return NetWorkManagerHolder.INSTANCE;
    }

    private NetWorkManager() {
        this.mOkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder()) //RetrofitUrlManager 初始化
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new DataEncryptInterceptor())//加密解密拦截
                .build();
        this.mDownloadOkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder()) //RetrofitUrlManager 初始化
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .build();

        this.mRetrofit = new Retrofit.Builder()
                .baseUrl(APP_DEFAULT_DOMAIN)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava
                .addConverterFactory(GsonConverterFactory.create())//使用Gson
                .client(mOkHttpClient)
                .build();

        this.mDownloadRetrofit = new Retrofit.Builder()
                .baseUrl(APP_DEFAULT_DOMAIN)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava
                .addConverterFactory(GsonConverterFactory.create())//使用Gson
                .client(mDownloadOkHttpClient)
                .build();

        this.mBleApiService = mRetrofit.create(BleApiService.class);
        this.mDownloadBleApiService = mDownloadRetrofit.create(BleApiService.class);

    }

    public OkHttpClient getOkHttpClient() {
        return mOkHttpClient;
    }

    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    public BleApiService getBleApiService() {
        return mBleApiService;
    }

    /**
     * 下载OkHttp
     * @return
     */
    public OkHttpClient getDownloadOkHttpClient() {
        return mDownloadOkHttpClient;
    }
    /**
     * 下载Retrofit
     * @return
     */
    public Retrofit getDownloadRetrofit() {
        return mDownloadRetrofit;
    }
    /**
     * 下载ApiService
     * @return
     */
    public BleApiService getDownloadBleApiService() {
        return mDownloadBleApiService;
    }
}
